package {{.PkgName}}

import (
	"net/http"

	"github.com/zeromicro/go-zero/rest/httpx"
	"blog-zero/api/internal/common/errorx"
	"blog-zero/common/validator"
	{{.ImportPackages}}
)

func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		{{if .HasRequest}}var req types.{{.RequestType}}
		if err := httpx.Parse(r, &req); err != nil {
			codeError := errorx.NewBusinessError(err.Error())
            httpx.WriteJsonCtx(r.Context(), w, http.StatusInternalServerError, codeError)
			return
		}

		validateErr := validator.Validate(&req)
		if validateErr != nil {
			codeError := errorx.NewpParamsValidateError(validateErr.Error())
			httpx.WriteJsonCtx(r.Context(), w, http.StatusInternalServerError, codeError)
			return
		}

		{{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx)
		{{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}&req{{end}})
		if err != nil {
			codeError := errorx.NewBusinessError(err.Error())
			httpx.WriteJsonCtx(r.Context(), w, http.StatusInternalServerError, codeError)
		} else {
			var body types.Body
			body.Msg = "OK"
			body.Code = "000000"
			body.Data = resp
			{{if .HasResp}}httpx.OkJsonCtx(r.Context(), w, body){{else}}httpx.Ok(w){{end}}
		}
	}
}
